<template>
  <div>
    <div class="big_div">
      <div class="left">
        <div class="project">
          <div class="image">
            <img width="100%" :src="img" alt="" />
          </div>
          <div class="text">
            <div class="intro" style="font-weight: bold">
              自习室房间名：{{ name }}
            </div>
            <div class="intro">自习室房间类型：{{ type }}</div>
          </div>
        </div>
      </div>
      <div class="center" :class="[read === true ? 'readOnly' : '']">
        <div
          class="div_row"
          v-for="(row, rowIndex) in seatData"
          :key="rowIndex"
        >
          <div
            class="row_content"
            v-for="(data, $index) in row"
            @click="closeRow(rowIndex, $index, data.isTrue, data.typeId)"
            :key="$index"
          >
            <img :src="data.pic" />
          </div>
        </div>
      </div>

      <div class="right" v-show="chooseSeat">
        <div>
          <el-date-picker
                  v-model="inTime"
                  type="datetimerange"
                  format="yyyy-MM-dd HH:mm:ss"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  range-separator="至"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间">
          </el-date-picker>
          <h3>&nbsp;</h3>
          <strong style="font-size: 24px">当前选择:</strong>&nbsp;
          <strong style="font-size: 24px">第{{ row }}排</strong>&nbsp;
          <strong style="font-size: 24px">第{{ column }}列</strong>&nbsp;&nbsp;&nbsp;&nbsp;
          <strong style="font-size: 24px">每小时{{ price }}元</strong>
          <div class="btn">
            <el-button type="primary" @click="sureChoose">确认选择</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getRoomDetail, getSeatList, add } from "@/utils/request";
export default {
  props: {
    read: {
      type: Boolean,
      default: false,
    },
  },
  name: "RoomDetail",
  data() {
    return {
      rows: null,
      columns: null,
      seatData: [],
        inTime: [],
      img: "",
      name: "",
      type: "",
      row: null,
      column: null,
      chooseSeat: false,
      num: 0,
      seatId: null,
      nom1: "https://s3.bmp.ovh/imgs/2022/03/81dbe99f2006e757.png",
      nom2: "https://s3.bmp.ovh/imgs/2022/03/8ce83f30c8c99829.png",
      vip1: "https://s3.bmp.ovh/imgs/2022/03/44c1ecbc31382114.png",
      vip2: "https://s3.bmp.ovh/imgs/2022/03/0b926962949d98e2.png",
      price: 0,
    };
  },
  created() {
    // 在页面加载的时候拿到数据
    // 拿到从上一个页面传过来的id值
    this.id = this.$route.query.id;
    this.findDetails();
  },
  methods: {
    open() {
      this.$message({
        type: "error",
        message: `提示: 抱歉!一次只能选择一个座位`,
      });
    },
    check() {
      this.$message({
        type: "error",
        message: `提示: 抱歉!当前座位已被占用`,
      });
    },
    addRow() {
      this.seatData = [];
      for (let a = 0; a < this.columns; a++) {
        let rowArr = [];
        for (let i = 0; i < this.rows; i++) {
          let rowObj = {};
          rowObj.isTrue = false;
          rowObj.pic = this.nom1;
          rowObj.typeId = 4;
          rowArr.push(rowObj);
        }
        this.seatData.push(rowArr);
      }
    },
    closeRow(rowIndex, $index, isTrue, typeId) {
      getSeatList({
        roomId: this.id,
        row: rowIndex + 1,
        column: $index + 1,
      }).then((res) => {
        if (res.rows[0].sta === 1) {
          this.check();
          return false;
        } else {
          if (this.num > 0) {
            if ((rowIndex === this.row - 1) & ($index === this.column - 1)) {
              this.seatData[rowIndex][$index].isTrue = !isTrue;
              if (typeId == 5) {
                this.seatData[rowIndex][$index].pic = this.vip1;
              } else {
                this.seatData[rowIndex][$index].pic = this.nom1;
              }
              this.row = null;
              this.column = null;
              this.chooseSeat = false;
              this.num = 0;
              return false;
            } else {
              this.open();
              return false;
            }
          } else {
            this.seatData[rowIndex][$index].isTrue = !isTrue;
            if (typeId == 5) {
              this.seatData[rowIndex][$index].pic = this.vip2;
            } else {
              this.seatData[rowIndex][$index].pic = this.nom2;
            }
            this.row = rowIndex + 1;
            this.column = $index + 1;
            this.chooseSeat = true;
            this.num = this.num + 1;
            this.seatId = res.rows[0].id;
            this.price = res.rows[0].price;
          }
        }
      });
    },
    findDetails() {
      getRoomDetail(this.id).then((res) => {
        this.rows = res.data.rows;
        this.columns = res.data.columns;
        this.img = res.data.pic;
        this.name = res.data.name;
        this.type = res.data.type;
        this.seatList();
        this.addRow();
      });
    },
    seatList() {
      getSeatList({ roomId: this.id }).then((res) => {
        res.rows.forEach((element) => {
          if (element.sta === 1) {
            if (element.typeId == 5) {
              this.seatData[element.row - 1][element.column - 1].pic =
                this.vip2;
              this.seatData[element.row - 1][element.column - 1].typeId = 5;
            }
            if (element.typeId == 4) {
              this.seatData[element.row - 1][element.column - 1].pic =
                this.nom2;
            }
          } else {
            if (element.typeId == 5) {
              this.seatData[element.row - 1][element.column - 1].pic =
                this.vip1;
              this.seatData[element.row - 1][element.column - 1].typeId = 5;
            }
            if (element.typeId == 4) {
              this.seatData[element.row - 1][element.column - 1].pic =
                this.nom1;
            }
          }
        });
      });
    },
    sureChoose() {
      let obj = {
        roomId: this.id,
        seatId: this.seatId,
          startTime:this.inTime[0],
        endTime:this.inTime[1]
      };
      add(obj).then((res) => {
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: `选座成功`,
          });
        }
      });
    },
  },
};
</script>

<style scoped lang='scss'>
.big_div {
  display: flex;
  padding-top: 50px;
  width: 1500px;
  height: 420px;
  .left {
    width: 600px;
    height: 420px;
    padding-left: 20px;
    .project {
      display: flex;
      padding: 1em 0;
      .image {
        width: 300px;
        height: 200px;
        background-color: #f4f4f4;
        border-radius: 5px;
        overflow: hidden;
      }
      .text {
        flex: 1;
        padding: 0 2em;
        .name {
          font-weight: bolder;
          font-size: 1em;
        }
        .date {
          margin: 1em 0;
        }
      }
    }
  }
  .center {
    width: 300px;
    height: 420px;
    .div_row {
      .row_content {
        width: 24px;
        height: 24px;
        border: 1px dotted rgba(230, 230, 230, 0.25);
        display: inline-block;
        margin: 5px 5px;
      }
    }
  }
  .right {
    width: 400px;
    height: 420px;
    display: flex;
    padding-left: 100px;
    .btn {
      width: 98px;
      height: 40px;
      padding-top: 5px;
      padding-left: 5px;
    }
  }
  .readOnly {
    pointer-events: none;
  }
}
</style>
